package com.crm.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.BusinessFollowDto;
import com.crm.entity.Business;
import com.crm.entity.ClueRecord;
import com.crm.entity.Pool;
import com.crm.entity.Record;
import com.crm.mapper.PoolMapper;
import com.crm.mapper.RecordMapper;
import com.crm.result.PageResult;
import com.crm.result.PageResultTow;
import com.crm.result.Result;
import com.crm.service.RecordService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RecordServiceImpl implements RecordService{
    @Autowired
    private RecordMapper recordMapper;

    /**
     * 新增商机跟进
     * @param businessFollowDto
     */
    @Override
    public void insertRecord(BusinessFollowDto businessFollowDto) {
        Record record = new Record();
        BeanUtils.copyProperties(businessFollowDto, record);
        record.setBusinessId(businessFollowDto.getBusinessId());
        record.setActivityId(businessFollowDto.getActivityId());
        record.setCourseId(businessFollowDto.getCourseId());
        record.setNextTime(businessFollowDto.getNextTime());
        record.setKeyItems(businessFollowDto.getKeyItems());
        record.setRecord(businessFollowDto.getRecord());
        String keyItems = businessFollowDto.getKeyItems();
        if (StrUtil.isNotBlank(keyItems)) {
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(keyItems);
            record.setKeyItemsArray(jsonArray.toString());
        } else {
            record.setKeyItemsArray("[]"); // 默认空数组
        }
        record.setOtherIntention(businessFollowDto.getOtherIntention());
        record.setTrackStatus(businessFollowDto.getTrackStatus());
        Business business = new Business();
        business.setId(Math.toIntExact(businessFollowDto.getBusinessId()));
        business.setCreateTime(businessFollowDto.getCreateTime());
        business.setName(businessFollowDto.getName());
        business.setPhone(businessFollowDto.getPhone());
        business.setProvinces(businessFollowDto.getProvinces());
        business.setQq(businessFollowDto.getQq());
        business.setWeixin(businessFollowDto.getWeixin());
        business.setSubject(businessFollowDto.getSubject());
        business.setSex(businessFollowDto.getSex());
        business.setStatus(businessFollowDto.getStatus());
        business.setAge(businessFollowDto.getAge());
        business.setChannel(businessFollowDto.getChannel());
        business.setCity(businessFollowDto.getCity());
        business.setOwner(businessFollowDto.getOwner());
        recordMapper.insertRecord(record);
    }

    @Override
    public Result getRecordsByBusinessId(Integer businessId) {
        List<Record> records = recordMapper.selectByBusinessId(businessId);
        return Result.success(records);

    }
}
